//app.js
App({
  onLaunch: function () {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
  },
  getUserInfoFirst: function () {
    let that = this;
    return new Promise(function (resolve, reject) {
      // 登录
      wx.login({
        success: res => {
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          // var that=this;
          if (res.code) {
            wx.request({
              url: that.buildUrl('/shop/shop/auto-login'),
              data: {
                code: res.code
              },
              method: 'GET',
              success: function (resq) {
                wx.setStorageSync('token', resq.data.data);
                wx.setStorageSync('status', resq.data.code);
                var res = {
                  status: 200,
                  data: '成功'
                }
                resolve(res);
              }
            })
          } else {
            // console.log('获取用户登录态失败！' + res.errMsg);
            that.showToast('获取用户登录态失败')
            var res = {
              status: 300,
              data: '错误'
            }
            reject('error');
          }
        },
        fail: err => {
          that.showToast('Login调用出错')
          var res = {
            status: 300,
            data: '错误'
          }
          reject('error');
        }
      })
    })
  },
  globalData: {
    addrnum: 1,
    unload: 1,
    userInfo: null,
    requestUri: 'https://xhrwx.pintuane.com'
    // requestUri: 'https://group.xcxstore.com'
    // requestUri: 'http://api.newgroup.com'
  },
  buildHeader: function () {
    let token = wx.getStorageSync('token');
    let timer, count = 0;
    if (token == ""){
      timer = setInterval(()=>{
        count++;
        token = wx.getStorageSync('token');
        if (token != "" || count == 5){
          clearInterval(timer)
          timer = null;
          count = 0;
        }
      },100)
    }
    return {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Auth': token
    };
  },
  buildUrl: function (url) {
    return this.globalData.requestUri + url;
  },
  showToast(str, icon = 'none') {
    wx.showToast({
      title: str,
      icon: icon,
      duration: 1200
    })
  },
  // 倒计时 - date(传入的结束时间), fun(执行后续操作), endFun(倒计时结束后的操作)
  countDown({ date: date, fun: fun, endFun: endFun }) {
    let timer = setInterval(() => {
      let now = new Date().getTime();
      let endDate = arguments[0].date;
      let time = endDate - now;
      let d = parseInt(time / 1000 / 60 / 60 / 24);
      let h = addZero(parseInt(time / 1000 / 60 / 60 % 24));
      let m = addZero(parseInt(time / 1000 / 60 % 60));
      let s = addZero(parseInt(time / 1000 % 60));
      if (arguments[0].fun) {
        let options = {
          d: d,
          h: h,
          m: m,
          s: s
        }
        arguments[0].fun(options)
      }
      if (time <= 0) {
        clearTimeout(timer);
        timer = null;
        if (arguments[0].endFun) {
          arguments[0].endFun()
        }
      }
    }, 1000)

    function addZero(num) {
      return num < 10 ? "0" + num : num;
    }
  },
  //时间 - date(传入时间) fun(执行后续操作)
  transformTime(date, fun) {
    let time = new Date(date);
    let options = {
      Y: time.getFullYear(),
      M: addZero(parseInt(time.getMonth() + 1)),
      D: addZero(parseInt(time.getDate())),
      h: addZero(parseInt(time.getHours())),
      m: addZero(parseInt(time.getMinutes())),
      s: addZero(parseInt(time.getSeconds())),
    }
    if (fun) {
      fun(options)
    }

    function addZero(num) {
      return num < 10 ? "0" + num : num;
    }
  }
});